package DataStr.Day2;

import java.util.HashMap;

public class Day2OneQuestion {
    public static void main(String[] args) {

    }
    //暴力解法
    public int[] twoSum(int[] nums, int target) {
        int oneIndex =0;
        int twoIndex = 0;
        int len = nums.length;
        int temp = 0;
        while(oneIndex < len-1){
            temp = target - nums[oneIndex];
            twoIndex = oneIndex+1;
            while(twoIndex < len){
                if(temp == nums[twoIndex]){
                    return new int[]{oneIndex,twoIndex};
                }
                twoIndex++;
            }
            oneIndex++;
        }

        return null;
    }

    //哈希表
    public int[] twoSumByHash(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
           if (map.containsKey(target - nums[i])){
               return new int[]{map.get(target - nums[i]),i};
           }
           map.put(nums[i],i);
        }

        return null;
    }
}
